package com.fare.taluo.action.crawl.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.crawl.FligthStatusInfoApplication;
import com.fare.taluo.application.crawl.GetNoFlightInfoLinesApplication;
import com.fare.taluo.queue.ExcelNoFlightInfo;
import com.fare.taluo.vo.crawl.FligthStatusInfoVO;
import com.fare.taluo.vo.crawl.statistics.StaticHistoryHbaseVO;
import com.fare.util.ReportProperties;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 生成没有航班信息Excel
 * @author admin
 *
 */
@Controller("crawlNoFlightInfoLinesAction")
@Scope("prototype")
@Namespace("/")
public class CrawlNoFlightInfoLinesAction extends BaseFdcAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Resource(name="getNoFlightInfoLinesApplication")	
	private GetNoFlightInfoLinesApplication getNoFlightInfoLinesApplication;
	
	@Resource(name ="fligthStatusInfoApplication")
	private FligthStatusInfoApplication fligthStatusInfoApplication ;
	
	private String loginTimeStr;
	
	private List<FligthStatusInfoVO> listVos;
	
	public String toJsp() {
		listVos=fligthStatusInfoApplication.findAll();
		returnPageURL = "/fare/_noFlightInfoReport.jsp";
		return "dispatcher";
	}
	
	public String findByCondition(){
		String grabDateStr=request.getParameter("grabDateStr");
		listVos=fligthStatusInfoApplication.findByCondition(grabDateStr);
		returnPageURL = "/fare/_noFlightInfoReport.jsp";
		return "dispatcher";
	}
	
	public String execute() throws Exception{
		Map<String ,List<StaticHistoryHbaseVO>> mapList=Maps.newHashMap();
		List<StaticHistoryHbaseVO> listVo=getNoFlightInfoLinesApplication.getEntityList(loginTimeStr);
		if(listVo.size()>0){
			//根据渠道分组
			for(StaticHistoryHbaseVO vo:listVo){
				String chaanelId=vo.getGrabChannelId();
				if(mapList.get(chaanelId)!=null){
					mapList.get(chaanelId).add(vo);
				}else{
					List<StaticHistoryHbaseVO> listV=Lists.newArrayList();
					listV.add(vo);
					mapList.put(chaanelId, listV);
				}
			}
			//生成EXCEL
			if(mapList.size()>0){
				//文件名
				String title="没有航班信息表";
				//路径
				String strFilePath = ReportProperties.getInstance().getSavePath().endsWith("/")? ReportProperties.getInstance().getSavePath() :	ReportProperties.getInstance().getSavePath()+"/";
				ExcelNoFlightInfo.noInfoExcel(mapList,title,strFilePath);
				//对应的文件信息入库
				FligthStatusInfoVO vo=new FligthStatusInfoVO();
				vo.setGrabDateStr(loginTimeStr);
				vo.setPath(strFilePath+title);
				vo.setTitle(title+loginTimeStr);
				fligthStatusInfoApplication.save(vo);
				return "SUCCESS";	
			}
		}
		return "没有对应的数据！";
	}
	
	
	
	//手动下载报表Action(单表下载)
	public void download(){
		//传报表的地址
		String filePath=request.getParameter("filePaths");
		String subjectName=request.getParameter("subjectNames");
		try {
			filePath = new String(filePath.getBytes("iso-8859-1"),"UTF-8");
			filePath=java.net.URLDecoder.decode(filePath,"UTF-8");
			subjectName = new String(subjectName.getBytes("iso-8859-1"),"utf-8");
			subjectName=java.net.URLDecoder.decode(subjectName,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		downOneFile(filePath,subjectName);
		
	}
	
	public void downOneFile(String filePath,String subjectName) {
		InputStream is = null;
		OutputStream out=null;
		try {
			File file = new File(filePath);  
			is = new FileInputStream(file); 
			String fileName = null;

			fileName = subjectName+".xls";
			out = response.getOutputStream();
			// response.reset();
			// 设置输出的格式
			// response.setContentType("application/pdf;charset=UTF-8");
			// HttpServletResponse response =
			response.setContentType("application/octet-stream;charset=UTF-8");
			if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0){
				fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");//firefox浏览器
			}else{
				fileName = URLEncoder.encode(fileName, "UTF-8");//其他浏览器
			}
			response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
			// 循环取出流中的数据
			byte[] b = new byte[1024*1024];
			int len;
			while ((len = is.read(b)) > 0){
				out.write(b, 0, len);
			}
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
			try {

				if (is != null) {
					is.close();
				}out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}

	}
	

	public String getLoginTimeStr() {
		return loginTimeStr;
	}
	public void setLoginTimeStr(String loginTimeStr) {
		this.loginTimeStr = loginTimeStr;
	}

	public List<FligthStatusInfoVO> getListVos() {
		return listVos;
	}

	public void setListVos(List<FligthStatusInfoVO> listVos) {
		this.listVos = listVos;
	}
	
	
}
